<template>
  <div>
    <el-row type="flex" justify="center" align="middle" class="login">
      <el-form :model="form" label-width="100px">
        <el-form-item label="用户名">
          <el-input type="username" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginHandle">提交</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import { userLogin } from "@/api";
import { setToken, setUserInfo } from "@/utils/myToken";
export default {
  data() {
    return {
      form: {
        username: "10010",
        password: "123",
      },
    };
  },
  methods: {
    // 点击登录
    loginHandle() {
      const { username, password } = this.form;
      if (!username || !password) {
        return this.$message.error("请输入完整信息");
      }
      // 调用登录接口，传入参数
      userLogin({ username, password }).then((res) => {
        console.log(res);
        if (res.data.message == "登录成功") {
          const { token, user } = res.data.data;
          //本地存储 token
          setToken(token);
          //本地存储 user
          setUserInfo(user);
          //跳到首页
          this.$router.replace("/");
          //登录成功的提示
          this.$message.success("欢迎入坑");
        } else {
          //失败提示
          this.$message.error("用户名或密码错误");
        }
      });
    },
    // 点击重置
    resetHandle() {
      // console.log("重置");
      this.form = { username: "", password: "" };
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100vw;
  height: 100vh;
  background-color: #f2f2f2;
  flex-direction: column;
}
</style>